using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using YushengMall.Backend.Api.Procedure;
using YushengMall.Backend.Api.Params;
using YushengMall.Backend.Api.Database;
using YushengMall.Backend.Api.Entity;
using System.Linq;
using YushengMall.Backend.Api.Repository;
using Microsoft.EntityFrameworkCore;
using YushengMall.Backend.Api.Common;

namespace YushengMall.Backend.Api.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class UsersController : ControllerBase
    {
        public readonly ExecProcedure exec = new ExecProcedure();
        //private readonly DbHelper _dbhelper;
        private readonly IRepository<Users> _userRepository;
        public UsersController(IRepository<Users> userRepository)
        {
            _userRepository = userRepository;
        }

        [HttpPost, Route("Login")]
        public dynamic UserLogin([FromBody]UserLogins logins)
        {


            int code;
            string msg;
            int data=0;

            var IsUserName = string.IsNullOrEmpty(logins.UserName.Trim());
            var IsPassword = string.IsNullOrEmpty(logins.Password.Trim());
            if (IsUserName || IsPassword)
            {
                code = 104;
                msg = "请输入用户名密码";
                data = 0;
            }
            else
            {
                // Dictionary<string, object> dict = new Dictionary<string, object>();
                // dict.Add("@UserName", logins.UserName);
                // dict.Add("@Password", logins.Password);
                //data = exec.ProcIsExistence(dict, "LoginWeb");
                data  = _userRepository.Table.Where(x=>x.UserName == logins.UserName.Trim() 
                && x.PassWord == logins.Password.Trim()).Count();

                //调用Authentication，返回token和refreshtoken
                
                     
                if (data > 0)
                {
                    code = 200;
                    msg = "登录成功";
                }
                else
                {
                    code = 104;
                    msg = "用户名密码错误";
                }

            }

            var res = new
            {
                Code = code,
                Data = data,
                Msg = msg
            };


            return res;
        }

        [HttpPost, Route("Register")]
        public dynamic RegisterWeb(UserReginster reginster)
        {
            int code;
            string msg;
            int data=0;

            var password = String.IsNullOrEmpty(reginster.Password) ? "" : reginster.Password;
            var phoneNumber = String.IsNullOrEmpty(reginster.PhoneNumber) ? "" : reginster.PhoneNumber;
            var repassword = String.IsNullOrEmpty(reginster.Repassword) ? "" : reginster.Repassword;
            var verificationCode = String.IsNullOrEmpty(reginster.VerificationCode) ? "" : reginster.VerificationCode;


            if (string.IsNullOrEmpty(password) || string.IsNullOrEmpty(repassword) || string.IsNullOrEmpty(phoneNumber) || string.IsNullOrEmpty(verificationCode))
            {
                code = 104;
                msg = "请确认信息完整再尝试注册";
                data = 0;
            }
            else
            {
                if (repassword == password && verificationCode == "")
                {
                    // Dictionary<string, object> dict = new Dictionary<string, object>();
                    // dict.Add("@PhoneNumber", phoneNumber);
                    // dict.Add("@Password", password);

                    //data = exec.ProcUpdateData(dict, "registerWeb");


                    var user = new Users()
                    {
                        UserName = phoneNumber,
                        NickName = phoneNumber,
                        PassWord = password,
                        PhoneNumber = phoneNumber,
                        CreateTime = DateTime.Now,
                        UpdateTime = DateTime.Now
                    };
                //插入数据并返回受影响的行数
                    data = _userRepository.Insert(user);

                    if (data > 0)
                    {
                        code = 200;
                        msg = "注册成功！";
                    }
                    else
                    {
                        code = 104;
                        msg = "注册失败！";
                    }

                }
                else if (repassword != password)
                {
                    code = 104;
                    msg = "两次密码输入不一致";
                    data = 0;
                }
                else
                {
                    code = 104;
                    msg = "验证码输入错误";
                    data = 0;
                }

            }

            var res = new
            {
                Code = code,
                Data = data,
                Msg = msg
            };


            return res;
        }


    }
}